var village = new Vue({
    el: '#village',
    data: {
        selected: [],
        dataAreas: {}
    },
    computed: {
        // 计算属性的 getter
        areas: function () {
            if (this.dataAreas == null) return [];
            let areas = [];
            for (let id in this.dataAreas) {
                areas.push(this.dataAreas[id]);
            }
            return areas;
        }
    }
});
var bindMultiAreaId = new Vue({
    el: '#bindMultiAreaId',
    data: {
        selected: [],
        dataBindAreaArr: {}
    },
    computed: {
        // 计算属性的 getter
        bindAreaArr: function () {
            if (this.dataBindAreaArr == null) return [];
            let bindAreaArr = [];
            for (let id in this.dataBindAreaArr) {
                bindAreaArr.push(this.dataBindAreaArr[id]);
            }
            return bindAreaArr;
        }
    }
});
$().ready(function () {
    $.post('/mp/repairBossBindAreas/loadAllArea', {
        cityId: $("#cityId").val(),
        userId: $("#userId").val()
    }, function (data) {
        village.dataAreas = data.dataAreas;
        bindMultiAreaId.dataBindAreaArr = data.dataBindAreaArr;
    }, 'json');
});

function doSubmit() {
    if (!!!bindMultiAreaId.bindAreaArr) return;
    let bindAreaIdArr = [];
    for (let bindArea of bindMultiAreaId.bindAreaArr) {
        bindAreaIdArr.push(bindArea.id);
    }
    $.ajax({
        cache: true,
        type: "POST",
        url: "/mp/repairBossBindAreas/bindArea",
        data: {bindAreaIdArr: bindAreaIdArr, userId: $("#userId").val()},
        async: false,
        error: function (request) {
            parent.layer.alert("Connection error");
        },
        success: function (data) {
            console.log(data);
            if (data.code == 0) {
                parent.layer.msg("操作成功");
                parent.reLoad();
                var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
                parent.layer.close(index);
            } else {
                parent.layer.alert(data.msg)
            }
        }
    });
}

function toRight() {
    let idArr = village.selected;
    if (idArr.length > 0) {
        for (let id of idArr) {
            Vue.set(bindMultiAreaId.dataBindAreaArr, id, village.dataAreas[id]);
            Vue.delete(village.dataAreas, id);
        }
    }
}

function toLeft() {
    let idArr = bindMultiAreaId.selected;
    if (idArr.length > 0) {
        for (let id of idArr) {
            Vue.set(village.dataAreas, id, bindMultiAreaId.dataBindAreaArr[id]);
            Vue.delete(bindMultiAreaId.dataBindAreaArr, id);
        }
    }
}

function allToRight() {
    if (!!!village.dataAreas) return;
    bindMultiAreaId.dataBindAreaArr = Object.assign({}, bindMultiAreaId.dataBindAreaArr, village.dataAreas);
    village.dataAreas = {};
}

function allToLeft() {
    if (!!!bindMultiAreaId.dataBindAreaArr) return;
    village.dataAreas = Object.assign({}, village.dataAreas, bindMultiAreaId.dataBindAreaArr);
    bindMultiAreaId.dataBindAreaArr = {};
}